using Lzfy_His_Service.Models;
using Lzfy_His_Service.Services;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Lzfy_His_Service.Controllers
{
    /// <summary>
    /// 退费任务工作量查询控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class RefundTaskWorkloadController : ControllerBase
    {
        private readonly IRefundTaskWorkloadService _refundTaskWorkloadService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="refundTaskWorkloadService">退费任务工作量服务</param>
        public RefundTaskWorkloadController(IRefundTaskWorkloadService refundTaskWorkloadService)
        {
            _refundTaskWorkloadService = refundTaskWorkloadService;
        }

        /// <summary>
        /// 获取所有用户的实时退费任务工作量数据
        /// </summary>
        /// <returns>退费任务工作量数据列表</returns>
        [HttpPost("all")]
        public async Task<ActionResult<ApiResponse<List<RefundTaskWorkload>>>> GetAllUserWorkload()
        {
            try
            {
                var data = await _refundTaskWorkloadService.GetAllUserWorkloadAsync();
                return Ok(new ApiResponse<List<RefundTaskWorkload>>
                {
                    Success = true,
                    Data = data,
                    Message = "查询成功"
                });
            }
            catch (System.Exception ex)
            {
                return StatusCode(500, new ApiResponse<List<RefundTaskWorkload>>
                {
                    Success = false,
                    Message = $"查询失败：{ex.Message}"
                });
            }
        }

        /// <summary>
        /// 获取指定用户的实时退费任务工作量数据
        /// </summary>
        /// <param name="request">用户名请求</param>
        /// <returns>退费任务工作量数据</returns>
        [HttpPost("user")]
        public async Task<ActionResult<ApiResponse<RefundTaskWorkload>>> GetUserWorkload([FromBody] UsernameRequest request)
        {
            if (string.IsNullOrWhiteSpace(request?.Username))
            {
                return BadRequest(new ApiResponse<RefundTaskWorkload>
                {
                    Success = false,
                    Message = "用户名不能为空"
                });
            }

            try
            {
                var data = await _refundTaskWorkloadService.GetUserWorkloadAsync(request.Username);
                if (data == null)
                {
                    return Ok(new ApiResponse<RefundTaskWorkload>
                    {
                        Success = false,
                        Message = "未找到该用户的退费任务工作量数据"
                    });
                }

                return Ok(new ApiResponse<RefundTaskWorkload>
                {
                    Success = true,
                    Data = data,
                    Message = "查询成功"
                });
            }
            catch (System.Exception ex)
            {
                return StatusCode(500, new ApiResponse<RefundTaskWorkload>
                {
                    Success = false,
                    Message = $"查询失败：{ex.Message}"
                });
            }
        }

        /// <summary>
        /// 获取退费任务工作量排名前N的用户
        /// </summary>
        /// <param name="request">排名请求</param>
        /// <returns>退费任务工作量数据列表</returns>
        [HttpPost("top")]
        public async Task<ActionResult<ApiResponse<List<RefundTaskWorkload>>>> GetTopWorkloadUsers([FromBody] TopUsersRequest request)
        {
            var topN = request?.TopN ?? 10;
            if (topN <= 0)
            {
                return BadRequest(new ApiResponse<List<RefundTaskWorkload>>
                {
                    Success = false,
                    Message = "topN参数必须大于0"
                });
            }

            try
            {
                var data = await _refundTaskWorkloadService.GetTopWorkloadUsersAsync(topN);
                return Ok(new ApiResponse<List<RefundTaskWorkload>>
                {
                    Success = true,
                    Data = data,
                    Message = "查询成功"
                });
            }
            catch (System.Exception ex)
            {
                return StatusCode(500, new ApiResponse<List<RefundTaskWorkload>>
                {
                    Success = false,
                    Message = $"查询失败：{ex.Message}"
                });
            }
        }
    }

    /// <summary>
    /// 用户名请求模型
    /// </summary>
    public class UsernameRequest
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string Username { get; set; } = string.Empty;
    }

    /// <summary>
    /// 排名请求模型
    /// </summary>
    public class TopUsersRequest
    {
        /// <summary>
        /// 前N名
        /// </summary>
        public int TopN { get; set; } = 10;
    }

    /// <summary>
    /// API响应模型
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    public class ApiResponse<T>
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 数据
        /// </summary>
        public T? Data { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; } = string.Empty;
    }
}